package com.liaoyifan.core.aspect;

import com.liaoyifan.core.constant.Constants;
import com.liaoyifan.core.constant.Properties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SuppressWarnings("unused")
@EnableAspectJAutoProxy(exposeProxy = true)
@Configuration
public class AspectConfiguration {

    @Bean
    @ConditionalOnProperty(name = Properties.DATASOURCE_ENABLE, havingValue = Constants.TRUE)
    public DataSourceAspect DataSourceAspect() {
        return new DataSourceAspect();
    }

    @Bean
    @ConditionalOnProperty(name = Properties.WEB_ASPECT_ENABLE, havingValue = Constants.TRUE)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public WebAspect webAspect() {
        return new WebAspect();
    }

    @Bean
    @ConditionalOnProperty(name = Properties.RATE_LIMIT_ASPECT_ENABLE, havingValue = Constants.TRUE)
    public RateLimitAspect rateLimitAspect() {
        return new RateLimitAspect();
    }

    @Bean
    @ConditionalOnProperty(
            name = Properties.PREVENT_DUPLICATE_ASPECT_ENABLE,
            havingValue = Constants.TRUE)
    public PreventDuplicateSubmitAspect preventDuplicateSubmitAspect() {
        return new PreventDuplicateSubmitAspect();
    }

    @Bean
    @ConditionalOnProperty(name = Properties.IDEMPOTENT_ASPECT_ENABLE, havingValue = Constants.TRUE)
    public IdempotentAspect idempotentAspect() {
        return new IdempotentAspect();
    }

    @Bean
    @ConditionalOnProperty(
            name = Properties.DISTRIBUTED_LOCK_ASPECT_ENABLE,
            havingValue = Constants.TRUE)
    public DistributedLockAspect distributedLockAspect() {
        return new DistributedLockAspect();
    }
}
